/**
 * create by young (young2f@outlook.com) on 2023/5/6
 * 关键点位
 */
'use strict';
const gjPoints = [
    '杀菌温度',
    '产品管道流量'
];
let sjwd = 0;
let sjwdRand = 8;
let chgd = 0;
let chgdRand = 6;

const _ = require('lodash');
setInterval(() => {
    main();
}, 1000 * 60 * 30);
main();

function main() {
    var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
    var req = new XMLHttpRequest();
    req.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            // console.log(req.responseText);
            const res = JSON.parse(req.responseText);
            const {result} = res;
            const {devices} = result;
            devices.forEach(device => {
                sendData(device);
            });

        }
    };
    req.open('POST', 'http://127.0.0.1:18008/api/device/find/page', true);
    req.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
    req.send(JSON.stringify({tenantCode: 'TEN23042822280074F9', skip: 1, limit: 200}));
}

function rand(min, max) {
    return Math.ceil(Math.random() * (max - min + 1) + min - 1);
}

//
function getPoint(name, len) {
    console.log(`sjwd:${sjwd} sjwdRand:${sjwdRand},chgd:${chgd}, chgdRand:${chgdRand}`);
    // if(_.includes(gjPoints,item=>item===name)){
    //
    // }
    if (name === '杀菌温度' && sjwd == sjwdRand) {
        sjwd = 0;
        sjwdRand = rand(10, 20);
        console.log(name, '发送低值');
        return rand(10, 20);
    }
    if (name === '杀菌温度') {
        sjwd++;
        console.log(name, '发送 210');
        return 210;
    }
    if (name === '产品管道流量' && chgd == chgdRand) {
        chgd = 0;
        chgdRand = rand(10, 20);
        console.log(name, '发送低值');
        return rand(10, 20);
    }
    if (name === '产品管道流量') {
        chgd++;
        console.log(name, '发送 5010');
        return 5010;
    }
    return rand(0, Math.pow(10, len));
}


// function getPoint(name, len) {
//     console.log('发送 sendLow', name, sendLow);
//     if (sendLow === 0) {
//         const temp = rand(10, 20);
//         console.log('发送', name, temp);
//         return temp;
//     }
//
//     if (name === '杀菌温度') {
//         console.log('发送 211');
//         return 211;
//     }
//
//
//     if (name === '产品管道流量') {
//         console.log('发送 5010');
//         return 5010;
//     }
//
//     const temp = rand(10, 20);
//     console.log('发送 ', temp);
//     return temp;
// }

function getNomData(device) {
    const {name, type, len} = device;
    switch (type) {
        case 'bool':
            return rand(0, 1);
        case 'uni16':
            return rand(0, Math.pow(10, len));
        case 'uni8':
            return getPoint(name, len);
        default :
            return Math.floor(Math.random() * 9);
    }
}


function sendData(device) {

    const {points, deviceCode, deviceSpecCode, tenantCode} = device;

    const params = [];
    points.forEach(item => {
        const {name, path} = item;
        params.push({name, path, value: getNomData(item)});
    });


    var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
    var req = new XMLHttpRequest();
    req.onreadystatechange = function () {
        // console.log(this.status);
        if (this.readyState == 4 && this.status == 200) {
            // console.log('上报数据', req.responseText);
        }
    };
    req.open('POST', 'http://127.0.0.1:18008/api/device/data/create', true);

    req.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
    const temp = {deviceCode, deviceSpecCode, tenantCode, params};
    console.log(temp)
    req.send(JSON.stringify({deviceCode, deviceSpecCode, tenantCode, params}));
}
